<?php
include dirname(dirname(__FILE__))."/admin.php";
class main_act extends Admin
{
	
    public function init()
    {
		
    }
	
	public function index(){
		$tpl = new Template();
		$tpl->display();
	}
	
	public function get_file_list(){
		if($_GET['path']){
			$arr = array();
			$dh = opendir(APPCONTROL.$_GET['path']);
			while (($v = readdir($dh))!= false){
				if($v!='.'&&$v!='..'){
					$pfile = APPDATA.'control/'.$_GET['path'].'.php';
					if(is_file($pfile)){
						if(!$curcnf){
							$curcnf = include $pfile;
						}
						if(in_array(str_replace('.xml','',$v),$curcnf)||$v=='top.xml'||$v=='bottom.xml'||$v=='quick_button.xml') $v .= ',install';
					}
					$arr[] = $v;
				}
			}
			echo json_encode($arr);
		}
	}
	
	public function edit_file(){
		if($_POST){
			delete_slash($_POST);
			$parr = explode('/',$_POST['path']);
			if($_POST['act']=='save_file') $this->save_file();
		}
		$str = file_get_contents(APPCONTROL.$_GET['path']);
		preg_match('/<author>(.*)<\/author>/U',$str,$author);
		preg_match('/<remark>(.*)<\/remark>/U',$str,$remark);
		preg_match('/<title>(.*)<\/title>/U',$str,$title);
		preg_match_all('/<main>(.*)<\/main>/Us',$str,$title);
		preg_match('/<javascript>(.*)<\/javascript>/Us',$str,$javascript);
		preg_match('/<style>(.*)<\/style>/Us',$str,$style);
		if($javascript[1]){
			$plus_name['javascript'] = $javascript[1];
		}
		if($style[1]){
			$plus_name['style'] = $style[1];
		}
		$tag = array('before','after','result');
		foreach($tag as $t){
			preg_match_all('/<'.$t.'>(.*)<\/'.$t.'>/Us',$str,$plus);
			if($plus[1]){
				foreach($plus[1] as $p){
					$key = $t;
					$plus_name[$key][] = str_replace(array('<=>'),array('<='),$p);
				}
			}
		}
		preg_match_all('/<before>(.*)<\/before>/Us',$str,$before);
		$str = preg_replace('/<(before|after|result|javascript|style)>.*<\/(before|after|result|javascript|style)>/Us','',$str);
		$plus_name['xml'] = $str;
		echo json_encode($plus_name);
	}
	public function new_file(){
		if($_POST['field_name']){
			$path = APPCONTROL.$_POST['path'].'/'.$_POST['field_name'].'.xml';
			if(is_file($path)){
				echo '<script type="text/javascript">alert("文件存在");</script>';
			}else{
				if(stripos($_POST['path'],'list/')!==false){
					$str = "<title>".$_POST['field_name']."</title>\n<value>\n{".$_POST['field_name']."}\n</value>";
					file_put_contents($path,$str);
					$this->install();
					echo '<script type="text/javascript">window.parent.closenewwindown();window.parent.listreload("'.str_replace('/','_',$_POST['path']).'");</script>';
				}else{
					$str = "<label>".$_POST['field_name']."</label>\n<main>\n<type>".$_POST['type']."</type>\n<field>".$_POST['field_name']."</field>\n</main>";
					file_put_contents($path,$str);
					$this->install();
					echo '<script type="text/javascript">window.parent.closenewwindown();window.parent.listreload("'.str_replace('/','_',$_POST['path']).'");</script>';
				}
			}
		}
	}
	public function install(){
		if($_POST['path']){
			if(stripos($_POST['path'],'/top.xml')!==false||stripos($_POST['path'],'/bottom.xml')!==false||stripos($_POST['path'],'/quick_button.xml')!==false) exit;
			$parr = explode("/",$_POST['path']);
			$path = $parr[0].'/'.$parr[1];
			$fname = str_replace('.xml','',$parr[2]);
			$pfile = APPDATA.'control/'.$path.'.php';
			if(is_file($pfile)){
				if(!$curcnf){
					$curcnf = include $pfile;
				}
				$add = 1;
				foreach($curcnf as $k=>$v){
					if($v==$fname){
						unset($curcnf[$k]);
						$add = 0;
					}
				}
				if($add==1) $curcnf[] = $fname;
				file_put_contents($pfile,"<?php\nreturn ".var_export($curcnf,true)."\n?>");
				$this->clear_cache($parr[1]);
			}
		}
	}
	public function save_file(){
		$parr = explode('/',$_POST['path']);
		$str = '';
		foreach($_POST as $k=>$v){
			$k = end(explode("_",$k));
			$v = trim(trim(trim(str_replace(array("<?php","?>","&lt;?php","?&gt;"),'',$v),"\r\n"),"\n"));
			if($k=='xml'){
				$str .= $v."\n";
			}elseif(stripos($k,'before')!==false){
				$str .= "<before>\n	".$v."	\n</before>\n";
			}elseif(stripos($k,'after')!==false){
				$str .= "<after>\n	".$v."	\n</after>\n";
			}elseif(stripos($k,'result')!==false){
				$str .= "<result>\n	".$v."	\n</result>\n";
			}elseif($k=='javascript'){
				$str .= "<javascript>\n	".$v."	\n</javascript>\n";
			}elseif($k=='style'){
				$str .= "<style>\n	".$v."	\n</style>\n";
			}
		}
		$path = APPCONTROL.$_POST['path'];
		if(!is_writable($path)&&is_file($path)){
			echo '<script type="text/javascript">alert("文件没权限写");</script>';
			exit;
		}
		file_put_contents(APPCONTROL.$_POST['path'],$str);
		$mod = $parr[count($parr)-2];
		$this->clear_cache($mod);
		exit;
	}
	public function sort_move(){
		$parr = explode('/',$_POST['path']);
		$path = $parr[0].'/'.$parr[1];
		$fname = str_replace('.xml','',$parr[2]);
		$pfile = APPDATA.'control/'.$path.'.php';
		$curcnf = include $pfile;
		$curkey = array_keys($curcnf,$fname);
		$curval = $curcnf[$curkey[0]];
		if($_GET['t']=='sort_up'){
			$change = $curkey[0]-1;
			while(true){
				if($curcnf[$change]) break;
				$change--;
			}
			if($curcnf[$curkey[0]]&&$curcnf[$change]){
				$curcnf[$curkey[0]] = $curcnf[$change];
				$curcnf[$change] = $curval;
			}
		}
		if($_GET['t']=='sort_down'){
			$change = $curkey[0]+1;
			while(true){
				if($curcnf[$change]) break;
				$change++;
			}
			if($curcnf[$curkey[0]]&&$curcnf[$change]){
				$curcnf[$curkey[0]] = $curcnf[$change];
				$curcnf[$change] = $curval;
			}
		}
		file_put_contents($pfile,"<?php\nreturn ".var_export($curcnf,true)."\n?>");
		$mod = $parr[count($parr)-2];
		$this->clear_cache($mod);
	}
	private function clear_cache($mod,$type){
		$path = $_POST['path'];
		if(stripos($path,'list/')!==false||stripos($path,'search/')!==false){
			@unlink(APPPATH.'data/cache/control/'.$mod.'_list_cache');
			@unlink(APPPATH.'data/cache/control/'.$mod.'search_control_cache.xml');
		}elseif(stripos($path,'edit/')!==false){
			@unlink(APPPATH.'data/cache/control/'.$mod.'edit_control_cache.xml');
			@unlink(APPPATH.'data/cache/control/'.$mod.'_control_cache.xml');
		}
	}
	
}	
runapp();
?>